/**
 * 预定情况可视化工具
 * 将二维数组转换为直观的日程表显示
 */
export const bookingVisualizer = {
    /**
     * 转换预定数据化文本为可视
     * @param {number[][]} bookingData - 预定数据二维数组
     * @returns {string} 可视化的预定情况文本
     */
    convertToVisual(bookingData) {
        // 验证输入格式
        if (!Array.isArray(bookingData) || bookingData.length !== 7) {
            throw new Error('输入必须是包含7天数据的二维数组');
        }

        // 生成表头（小时刻度）
        let result = '      |';
        for (let hour = 0; hour < 24; hour++) {
            result += `${hour.toString().padStart(2, '0')}|`;
        }
        result += '\n';
        result += '-'.repeat(2 + 1 + 24 * 3) + '\n';

        // 生成每天的数据行
        const dayNames = ['今天', '明天', '后天', '第4天', '第5天', '第6天', '第7天'];

        bookingData.forEach((dayData, dayIndex) => {
            // 验证每天的数据格式
            if (!Array.isArray(dayData) || dayData.length !== 24) {
                throw new Error(`第${dayIndex + 1}天的数据必须包含24个小时`);
            }

            // 生成行标题（天数）
            result += `${dayNames[dayIndex].padEnd(6)}|`;

            // 生成每个小时的状态标记
            dayData.forEach(hourStatus => {
                // 0: 未预定（用空格表示），1: 已预定（用■表示）
                result += hourStatus === 1 ? '■ |' : '  |';
            });

            result += '\n';
        });

        return result;
    },

    /**
     * 转换预定数据为详细文本描述
     * @param {number[][]} bookingData - 预定数据二维数组
     * @returns {string} 详细的预定情况描述
     */
    convertToDescription(bookingData) {
        const descriptions = [];
        const dayNames = ['今天', '明天', '后天', '第4天', '第5天', '第6天', '第7天'];

        bookingData.forEach((dayData, dayIndex) => {
            const bookedHours = [];

            dayData.forEach((status, hour) => {
                if (status === 1) {
                    bookedHours.push(hour);
                }
            });

            if (bookedHours.length === 0) {
                descriptions.push(`${dayNames[dayIndex]}: 无预定`);
            } else {
                // 合并连续的时段
                const timeRanges = this.mergeTimeRanges(bookedHours);
                const rangeTexts = timeRanges.map(range => {
                    if (range.start === range.end) {
                        return `${range.start}:00`;
                    }
                    return `${range.start}:00-${range.end + 1}:00`;
                });

                descriptions.push(`${dayNames[dayIndex]}: 已预定 [${rangeTexts.join(', ')}]`);
            }
        });

        return descriptions.join('\n');
    },

    /**
     * 将小时数组合并为连续的时段
     * @param {number[]} hours - 小时数组
     * @returns {Array<{start: number, end: number}>} 合并后的时段
     */
    mergeTimeRanges(hours) {
        if (hours.length === 0) return [];

        // 排序并去重
        const sortedHours = [...new Set(hours)].sort((a, b) => a - b);
        const ranges = [];
        let currentRange = { start: sortedHours[0], end: sortedHours[0] };

        for (let i = 1; i < sortedHours.length; i++) {
            if (sortedHours[i] === currentRange.end + 1) {
                currentRange.end = sortedHours[i];
            } else {
                ranges.push(currentRange);
                currentRange = { start: sortedHours[i], end: sortedHours[i] };
            }
        }

        ranges.push(currentRange);
        return ranges;
    }
};

// 使用示例
const bookingData = [
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];

// 输出可视化表格
console.log('=== 预定情况可视化 ===');
console.log(bookingVisualizer.convertToVisual(bookingData));

// 输出详细描述
console.log('\n=== 预定情况详情 ===');
console.log(bookingVisualizer.convertToDescription(bookingData));